<?php
/**
 *快聚fun发起聚会
 */
include 'inc/common.inc.php';
include 'MParty.class.php';
include 'MCheckUser.class.php';
include 'SMS.class.php';
include 'authcode.class.php';

class Controller extends BaseController
{

	/**
	 *添加用户发起的聚会
	 */
	public function addpartyAction()
	{
		if($this->second == "false")
		{
			//创建party的module类
			$mp = new MParty();
			
			//获取前端参数
			$name = $this->get_request('name');
			$theme = $this->get_request('theme');
			$content = $this->get_request('content');
			$allAttend = $this->get_request('allAttend');
			$place = $this->get_request('place');
			$startTime = $this->get_request('startTime');
			$noteDate = $this->get_request('noteDate');
			$launchDate = $this->get_request('launchDate');
			$friends = $this->get_request('friends');
			$friendsArray = explode(";",$friends);
			$number = count($friendsArray);

			$party = array(
				'userTel'	=>	$_SESSION['userTel'],
				'name'		=>	$name,
				'theme'		=>	$theme,
				'content'	=>	$content,
				'allAttend'	=>	$allAttend,
				'place'		=>	$place,
				'number'	=>	$number,
				'startTime'	=>	$startTime,
				'noteDate'	=>	$noteDate,
			);
			
			//添加聚会信息
			$party_id = $mp->add_party($party);
			
			
			//添加联系人信息
			$mp->add_friends($friends);
			
			//添加可选日期
			$mp->add_alternative_date($launchDate, $party_id);
			
			//添加需要通知的人到短信队列
			$mp->add_friends_to_sms_queue($friends, $party_id);
			$authcode = new authcode();
			$gatherIdCode = $authcode->keycode($party_id,"ENCODE");
			$this->_msg->set(array('desc'=>$gatherIdCode,"return_code"=>1));
			echo $this->_msg;
			flush();
			
			//开始从短信队列中发短信
			$sms = new SMS();
			$friends = explode(";",$friends);
			if(count($friends)>0)
			{
				$name = array();
				$tels = array();
				
				foreach($friends as $friend)
				{
					$tmp = explode("#", $friend);
					$tel  = $tmp[0];
					$temp = $party_id."#".$tel;
					$encode = $authcode->keycode($temp,"ENCODE");
					//echo "{$party['name']}通过【快聚FUN】邀您参加\"{$party['theme']}\",请访问链接回复邀请 http://{$_SERVER['SERVER_NAME']}/quickfun/invitation.php?gather={$encode}";
					$sms->send_com_msg("{$party['name']}通过【快聚FUN】邀您参加\"{$party['theme']}\",请访问链接回复邀请 http://{$_SERVER['SERVER_NAME']}/quickfun/invitation.php?gather={$encode}",array($tel));					
				}
			}
			//$mp->send_sms_to_friends($party_id);
		}else{
			$this->_msg->set(array('desc'=>"请勿重复刷新","return_code"=>0));
			echo $this->_msg;
		}
	}
	
	public function clearCookieAction()
	{
		parent::clearCookie();
		$this->_msg->set(array("desc"=>"清理cookie成功","return_code"=>1));
		echo $this->_msg;
	}
}



$control = new Controller();
$control->run();
$dbCheck = new MCheckUser();

if(!$control->get_action_name() && $dbCheck->checkSessionId())
{
	//创建party的module类
	$mp = new MParty();
	//好友字符串，供自动补全插件调用
	$friends = $mp->get_friend_str();
	$userTel = $control->get_session("userTel");
	$contactPeople = $mp->contactPeople($userTel);
	$verify = $control->verify;
	//引入模版层
	include template('launch_party.html');
}else if(!$control->get_action_name() && !$dbCheck->checkSessionId())
{
	header("Location: index.php");
}
?>